package com.jsrj.common.wxService;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.jsrj.common.rediskey.RedisKey;
import com.jsrj.common.utils.sign.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.AlgorithmParameterSpec;

/**
 * 微信解密工具
 */

@Component
public class WxService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     *
     * @param encryptedDate: 加密数据
     * @param sessionKey
     * @param iv
     * @return
     * @throws Exception
     */
    public String wxDecrypt(String encryptedDate,String iv,String sessionKey) throws Exception {

        byte[] encData = cn.hutool.core.codec.Base64.decode(encryptedDate);
        byte[] vi = cn.hutool.core.codec.Base64.decode(iv);
        byte[] key = Base64.decode(sessionKey);
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(vi);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec keySpec = new SecretKeySpec(key,"AES");
        cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);
        return new String(cipher.doFinal(encData),"UTF-8");
    }



}
